package com.zlc.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlc.common.utils.R;
import com.zlc.gulimall.ware.feign.ProductFeignService;
import com.zlc.gulimall.ware.vo.HasStockVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlc.common.utils.PageUtils;
import com.zlc.common.utils.Query;

import com.zlc.gulimall.ware.dao.WareSkuDao;
import com.zlc.gulimall.ware.entity.WareSkuEntity;
import com.zlc.gulimall.ware.service.WareSkuService;

import javax.annotation.Resource;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Resource
    private WareSkuDao wareSkuDao;

    @Resource
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                new QueryWrapper<WareSkuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {

        LambdaQueryWrapper<WareSkuEntity> wrapper = new LambdaQueryWrapper<>();

        String skuId = (String) params.get("skuId");
        if(!StringUtils.isEmpty(skuId)){
            wrapper.eq(WareSkuEntity::getSkuId,skuId);
        }

        String wareId = (String) params.get("wareId");
        if(!StringUtils.isEmpty(wareId)){
            wrapper.eq(WareSkuEntity::getWareId,wareId);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //1.判断如果还没有这个库存记录新增
        List<WareSkuEntity> entities = wareSkuDao.selectList(
                new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if(entities == null || entities.size() == 0){
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);
            //别远程调用了，就这样吧
            wareSkuEntity.setSkuName("");
            wareSkuDao.insert(wareSkuEntity);
        }else {
            //2.如果有 就修改
            wareSkuDao.addStock(skuId,wareId,skuNum);
        }
    }

    @Override
    public List<HasStockVo> getSkusHhasStock(List<Long> skuIds) {

        List<WareSkuEntity> wareSkuEntities = this.list(
                new LambdaQueryWrapper<WareSkuEntity>().in(WareSkuEntity::getSkuId, skuIds)
        );
        List<HasStockVo> collect = wareSkuEntities.stream().map(item -> {
            HasStockVo hasStockVo = new HasStockVo();
            hasStockVo.setSkuId(item.getSkuId());
            hasStockVo.setStock(item.getStock());
            return hasStockVo;
        }).collect(Collectors.toList());

        return collect;
    }

}